package com.glodon.paas.document.dao.mysql;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.document.dao.ProjectDao;
import com.glodon.paas.document.dao.domain.ProjectInfoEntity;
import com.glodon.paas.document.dao.mapper.ProjectInfoMapper;
import com.glodon.paas.util.DateUtil;

public class ProjectInfoDaoMysqlImpl implements ProjectDao {

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Override
    public String insert(ProjectInfoEntity entity) {
        assert entity.getId() != null : "ProjectInfoEntity.getId() is null";
        // insert
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(DateUtil.getNow());
        }
        entity.setUpdateTime(entity.getCreateTime());
        projectInfoMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public ProjectInfoEntity get(String id) {
        return projectInfoMapper.selectByPrimaryKey(id);
    }
    @Override
    public List<ProjectInfoEntity> getProjects(List<String> ids) {
        return projectInfoMapper.selectProjectsByIds(ids);
    }

    @Override
    public int delete(String id) {
        return projectInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateProject(ProjectInfoEntity project) {
        return projectInfoMapper.updateByPrimaryKey(project);
    }

    @Override
    public List<ProjectInfoEntity> getProjects(String ownerId) {
        return projectInfoMapper.selectAllProjects(ownerId);
    }

    @Override
    public ProjectInfoEntity getProjectByName(String ownerId, String name) {
        return projectInfoMapper.getProjectByName(ownerId, name);
    }
    
    public void setProjectInfoMapper(ProjectInfoMapper projectInfoMapper) {
        this.projectInfoMapper = projectInfoMapper;
    }
}
